﻿package ru.vega_int.firsoff.as3.transition 
{
	import fl.transitions.easing.None;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import flash.display.DisplayObject;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	
	/**
	 * Класс fadeIn/fadeOut нужный DysplayObject
	 * @author Firsoff Maxim, icq:235859730
	 * @version 0.1/16.12.2008 13:31
	 */
	public class Fader
	{
		
		//	PROPERTIES
		//	public
		//	private
		//private static var _tweens:Array;
		//	const
		//	----------------------------------------------------------------------------------
		
		//	CONSTRUCTOR
		public function Fader() 
		{
			throw new IllegalOperationError('class has no instances');
		};
		
		//	METHODS
		//	private
		//	public
		/**
		 * Всплытие из альфы
		 * @param	target	Объект
		 * @param	duration	Длительность в секундах
		 */
		public static function fadeIn(target:DisplayObject, duration:Number):void 
		{
			//trace('FADE IN')
			//if (!_tweens) { _tweens = new Array(); };
			var t:Tween = new Tween(target, 'alpha', None.easeNone, target.alpha, 1, duration, true)
			//t.addEventListener(TweenEvent.MOTION_CHANGE, test);
			t.addEventListener(TweenEvent.MOTION_FINISH, handleTweenFinish);
			
		};
		
		/**
		 * Исчезновение через альфу
		 * @param	target	Объект
		 * @param	duration	Длительность в секундах
		 */
		public static function fadeOut(target:DisplayObject, duration:Number):void 
		{
			//if (!_tweens) { _tweens = new Array(); };
			var t:Tween = new Tween(target, 'alpha', None.easeNone, target.alpha, 0, duration, true);
			t.addEventListener(TweenEvent.MOTION_FINISH, handleTweenFinish);
			
		};
		//	----------------------------------------------------------------------------------
		
		//	GETTERS/SETTERS
		
		//	----------------------------------------------------------------------------------
		
		//	HANDLERS
		static private function handleTweenFinish(event:TweenEvent):void 
		{
			var t:Tween = Tween(event.target);
			t.removeEventListener(TweenEvent.MOTION_FINISH, handleTweenFinish);
			DisplayObject(t.obj).dispatchEvent(new Event(TweenEvent.MOTION_FINISH));
		};
		//	----------------------------------------------------------------------------------
	};
	
};